package cn.wisenergy.pai.monitor.linux.parser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


public class Memory implements Serializable{
	private static final long serialVersionUID = 8466615838661029931L;
	//空闲内存
	private long freeMem;
	//内存总量
	private long totalMem;
	//内存使用率
	private float usedMem;
	//可用内存
	private long avaliableMem;
	//交互内存总量
	private long swapTotal;
	//交互空余
	private long swapFree;

	public long getFreeMem() {
		return freeMem;
	}

	public void setFreeMem(long freeMem) {
		this.freeMem = freeMem;
	}

	public long getTotalMem() {
		return totalMem;
	}

	public void setTotalMem(long totalMem) {
		this.totalMem = totalMem;
	}

	public float getUsedMem() {
		return usedMem;
	}

	public void setUsedMem(float userdMem) {
		this.usedMem = userdMem;
	}

	public long getAvaliableMem() {
		return avaliableMem;
	}

	public void setAvaliableMem(long avaliableMem) {
		this.avaliableMem = avaliableMem;
	}

	public long getSwapTotal() {
		return swapTotal;
	}

	public void setSwapTotal(long s) {
		swapTotal = s;
	}

	public long getSwapFree() {
		return swapFree;
	}

	public void setSwapFree(long s) {
		swapFree = s;
	}

	@Override
	public String toString() {
		return "Mem [freeMem=" + freeMem + ", totalMem=" + totalMem + ", usedMem=" + usedMem + ", avaliableMem="
				+ avaliableMem + ", SwapTotal=" + swapTotal + ", SwapFree=" + swapFree + "]";
	}

	@Override
	public byte[] toBytes() throws IOException {
		ByteArrayOutputStream bytesOutput=new ByteArrayOutputStream();
		DataOutputStream dataOutput=new DataOutputStream(bytesOutput);
		dataOutput.writeLong(freeMem);
		dataOutput.writeLong(totalMem);
		dataOutput.writeFloat(usedMem);
		dataOutput.writeLong(avaliableMem);
		dataOutput.writeLong(swapTotal);
		dataOutput.writeLong(swapFree);
		byte[] result=bytesOutput.toByteArray();
		dataOutput.close();
		bytesOutput.close();
		return result;
	}
	@Override
	public void toObject(byte[] ab) throws IOException {
		ByteArrayInputStream bytesInput=new ByteArrayInputStream(ab);
		DataInputStream dataInput=new DataInputStream(bytesInput);
		if(dataInput.available()>0){
			freeMem=dataInput.readLong();
		}
		if(dataInput.available()>0){
			totalMem=dataInput.readLong();
		}
		if(dataInput.available()>0){
			usedMem=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			avaliableMem=dataInput.readLong();
		}
		if(dataInput.available()>0){
			swapTotal=dataInput.readLong();
		}
		if(dataInput.available()>0){
			swapFree=dataInput.readLong();
		}
		dataInput.close();
		bytesInput.close();
	}


}
